# -*- coding: utf-8 -*-
#高级特性----装饰器

import io
import sys
import functools
import time
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')


def log(func):
	@functools.wraps(func)
	def wapper(*args,**kw):
		print("call %s():" % func.__name__)
		return func(*args,**kw)
	return wapper

@log
def now():
	print("2015-3-25")

f = now
f()

print(now.__name__)


def log(text):
	def decorator(func):
		@functools.wraps(func)
		def wapper(*args,**kw):
			print('%s %s():' % (text, func.__name__))
			return func(*args,**kw)
		return wapper
	return decorator

@log('execute')
def now():
    print('2015-3-25')

now()


def metric(fn):
    began=time.time()
    @functools.wraps(fn)
    def decorator(*args,**kw):
        end=time.time()
        print('%s execueted in %s ms'%(fn.__name__,end-began))
        return fn(*args,**kw)
    return decorator

# 测试
@metric
def fast(x, y):
    time.sleep(0.0012)
    return x + y;

@metric
def slow(x, y, z):
    time.sleep(0.1234)
    return x * y * z;

f1 = fast(11, 22)
s1 = slow(11, 22, 33)
if f1 != 33:
    print('测试失败!')
elif s1 != 7986:
    print('测试失败!')